//
//  BiLoginViewController.m
//  TrunkIndustryManageSystem
//
//  Created by 李璐西 on 15-4-9.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiLoginViewController.h"

@interface BiLoginViewController ()<UITextFieldDelegate>
{
    UITextField * _phoneTextField;
    UITextField * _pwdTextField;

}
@end


@implementation BiLoginViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title=@"登录";
    }
    return self;
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    [self initView];
  
}
-(void)initView
{
    self.view.backgroundColor = UIColorFromRGB(0xe3e4e6);
    [self setNavBarTitle:@"登录"];
    [self setNavBarWithHidden:NO];
//    [self setNavBackBtn];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake((MAIN_SCREEN_WIDTH-97)/2, 64+25, 97, 84)];
    imageView.image = LOAD_IMAGE_WITH_CACHE(@"login/icon_logo.png");
    [self.view addSubview:imageView];
    
    // 员工号
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((MAIN_SCREEN_WIDTH-250)/2, 220, 250, 40)];
    imageView.image = LOAD_IMAGE_WITH_CACHE(@"login/icon_user.png");
    [self.view addSubview:imageView];
    
    CGRect rtPhoneText = imageView.frame;
    rtPhoneText.origin.x = (MAIN_SCREEN_WIDTH-250)/2 + 35;
    rtPhoneText.size.width = 220;
    _phoneTextField = [[UITextField alloc] initWithFrame:rtPhoneText];
    _phoneTextField.placeholder = @"员工号";
    _phoneTextField.textColor = [UIColor blackColor];
    _phoneTextField.font = [UIFont systemFontOfSize:16];
    _phoneTextField.text =@"liluxi";
    _phoneTextField.delegate = self;
    //    代理
    [_phoneTextField setDelegate:self];
    [self.view addSubview:_phoneTextField];
    //    FirstResponder
    
    // 密码
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((MAIN_SCREEN_WIDTH-250)/2, 279, 250, 40)];
    imageView.image = LOAD_IMAGE_WITH_CACHE(@"login/icon_password.png");
    [self.view addSubview:imageView];
    
    CGRect rtPwdText = imageView.frame;
    rtPwdText.origin.x = (MAIN_SCREEN_WIDTH-250)/2 + 35;
    rtPwdText.size.width = 220;
    _pwdTextField = [[UITextField alloc] initWithFrame:rtPwdText];
    _pwdTextField.placeholder = @"密码";
    _pwdTextField.textColor = [UIColor blackColor];
    _pwdTextField.font = [UIFont systemFontOfSize:16];
    _pwdTextField.secureTextEntry = YES;
    _pwdTextField.delegate = self;
    
    _pwdTextField.text= @"123456";
    //    代理
    [_pwdTextField setDelegate:self];
    [self.view addSubview:_pwdTextField];
    
    UIButton * loginBtn = GET_BUTTON(CGRectMake((MAIN_SCREEN_WIDTH-250)/2, 345, 250, 40), 17, NO, [UIColor whiteColor]);
    [loginBtn setTitle:@"登    录" forState:UIControlStateNormal];
    [loginBtn setBackgroundImage:LOAD_IMAGE_WITH_CACHE(@"login/icon_login.png") forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];

}
- (void)login:(UIButton*)button
{
    
    NSString * userName = _phoneTextField.text;
    NSString * pwd = _pwdTextField.text;
    
    [self asyshowHUDView:WAITING CurrentView:self];
    
    [[BiNetEngine shareInstance] requestLogin:self userName:userName passWord:pwd];

}


- (void)requestFinished:(ASIHTTPRequest *)request{

        NSString *responseStr = [request responseString];
    
        NSDictionary *jsonDict = [responseStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    BiUserEntity * entity = [BiUserEntity sharedInstance];
    
    entity.resultinfo = jsonDict[@"result_info"];
    
    entity.responseStatus = jsonDict[@"response_status"];
    
    id  loginDic = jsonDict[@"data"];
    
    if (loginDic && [loginDic isKindOfClass:[NSDictionary class]]) {
        
        entity.access_token = loginDic[@"access_token"];
        
        entity.userid = loginDic[@"id"];
        
        entity.name = loginDic[@"name"];
    }
    
    if ([entity.responseStatus integerValue] != 200) {
        
        [self showMessage:entity.resultinfo];
        
        return;
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma -mark  UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}

- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    [self removeHUDView:self];
    
    if ([entity isKindOfClass:[NSDictionary class]]) {
        
        [self showMessage:entity[@"result_info"]];
        
        return;
    }
    
//    BiUserEntity * entity_ = (BiUserEntity *)entity;
//    
//    if ([entity_.responseStatus integerValue] != 200) {
//        
//        [self showMessage:entity_.resultinfo];
//        
//        return;
//    }
//    
    [self.navigationController popViewControllerAnimated:YES];

}
- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    [self removeHUDView:self];
    
    [self showMessage:@"请求超时！"];
}

@end

